package com.demo.service.impl;


import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.demo.domain.vo.UserLoginVo;
import com.demo.utils.SnowFlakeUtil;
import com.demo.utils.exception.BizServiceException;
import com.demo.utils.redis.RedisTenantCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.demo.domain.entity.User;
import com.demo.mapper.UserMapper;
import com.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 学生信息表 服务实现类
 * </p>
 *
 * @author wu
 * @since 2025-06-20 14:06:18
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RedisTenantCache redisTenantCache;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        log.info("UserServiceImpl.findAll");
        log.info("查询列表开始");
        return userMapper.findAll();
    }

    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    @Override
    public boolean add(User user) {
        log.info("UserServiceImpl.add user:{}", JSON.toJSONString(user));
        boolean add = false;
        try {
            add = userMapper.add(user);
        } catch (Exception e) {
            log.error("UserServiceImpl.add error:{}", e.getMessage(), e);
            throw new BizServiceException("新增失败");
        }

        log.info("UserServiceImpl.add result:{}", add);
        return add;
    }

    @Override
    public boolean update(User user) {
        return userMapper.update(user);
    }

    public boolean deleteByid(Long id) {
        return userMapper.deleteByid(id);
    }

    @Override
    public Boolean register(UserLoginVo userLoginVo) {
        log.info("UserServiceImpl.register userLoginVo:{}", JSON.toJSONString(userLoginVo));
        SnowFlakeUtil snowFlakeUtil = new SnowFlakeUtil(1, 1);
        long id = snowFlakeUtil.nextId();
        //账号不能重复
        //查询账号
        Integer count = userMapper.findCountByUserName(userLoginVo.getUserName());
        if (count != 0) {
            log.info("UserServiceImpl.register 账号已存在");
            throw new BizServiceException("账号已存在");
        }
        User user = BeanUtil.copyProperties(userLoginVo, User.class);
        user.setId(id);
        return this.save(user);
    }

    @Override
    public String getCode() {
        int num = (int)(Math.random() * 9 + 1);
        String smsCode = num + RandomStringUtils.randomNumeric(5);
        redisTenantCache.setCacheObject("admin:code", smsCode, 60, TimeUnit.SECONDS);

        return smsCode;
    }

    @Override
    public Boolean code(String code) {
        log.info("UserServiceImpl.code code:{}",code);
        String s = redisTenantCache.getCacheString("admin:code");
        if (redisTenantCache.keyIsExists("admin:code")){
            throw new BizServiceException("验证码已经失效");
        }
        if (code.equals(s)){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean setCode(String code) {
        return null;
    }
}
